import useStatus from "./useStatus";
import useUpdateStatus from "./useUpdateStatus";
import useRemove from "./useRemove";

import { statusOptions } from "@/const";
import { Space } from "antd";
import moment from "moment";

export default ({
  renderEdit,
  renderShowEdit,
  renderShowRemove,
  renderModalForm,
  updateStatus,
  remove,
  actionRef,
  renderAction,
  insertPosition = 2,
  renderOtherAction,
  actionColumnWidth,
  scroll,
  onlyAction = false,
  hiddenAction = false,
}) => {
  const myRenderAction = (_, record) => {
    const isShowEdit = renderShowEdit
      ? renderShowEdit(record, actionRef)
      : null;
    const isShowRemove = renderShowRemove
      ? renderShowRemove(record, actionRef)
      : null;

    const arr = [
      renderEdit
        ? renderEdit(record, actionRef)
        : isShowEdit
        ? renderModalForm(2, record)
        : null,
      updateStatus ? useUpdateStatus(updateStatus, record, actionRef) : null,
      remove && isShowRemove ? useRemove(remove, record, actionRef) : null,
    ];
    if (renderOtherAction) {
      const result = renderOtherAction(_, record, actionRef);
      if (Array.isArray(result)) {
        arr.splice(insertPosition, 0, ...result);
      } else {
        arr.splice(insertPosition, 0, result);
      }
    }
    if (renderAction) {
      return renderAction(_, record, renderModalForm, actionRef);
    } else {
      return <Space>{arr.map((item) => item)}</Space>;
    }
  };

  const columns = [
    {
      title: "备注",
      dataIndex: "remark",
      width: 150,
      search: false,
      ellipsis: true,
    },
    {
      title: "创建时间",
      dataIndex: "createTime",
      width: 200,
      search: false,
      render: (_, record) =>
        moment(record.createTime * 1000).format("YYYY-MM-DD HH:mm:ss"),
    },
    {
      title: "修改时间",
      dataIndex: "updateTime",
      width: 200,
      search: false,
      render: (_, record) =>
        record.updateTime
          ? moment(record.updateTime * 1000).format("YYYY-MM-DD HH:mm:ss")
          : "-",
    },
    {
      title: "状态",
      dataIndex: "status",
      valueType: "select",
      fieldProps: {
        options: statusOptions,
      },
      render: useStatus,
    },
    {
      title: "操作",
      fixed: scroll ? "right" : false,
      dataIndex: "option",
      valueType: "option",
      width: actionColumnWidth || 200,
      render: myRenderAction,
    },
  ];

  // 没有启用/禁用方法，则不显示启用/禁用和状态列
  if (!updateStatus) {
    columns.splice(3, 1);
  }

  // 隐藏操作列
  if (hiddenAction) {
    columns.splice(-1, 1);
  }
  return onlyAction ? columns.slice(-1) : columns;
};
